/**
 ******************************************************************************
 * @file    
 * @author 
 * @Version 
 * @Date    
 * @brief 
 *******************************************************************************  
 **/
#include "zb32l03x_hal.h"
#include "log.h"
#include "bsp_key.h"
#include "bsp_powerctrl.h"
#include "app_key.h"
#include "bsp_loop.h"
#include "Ringbuff.h"
#include "FreeRTOS.h"
#include "ui_lcd.h"



/*
*********************************************************************************************************
*	功能说明: 初始化
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/

void app_key_init(KEY_t *key)
{
   uint8_t i;
   bsp_key_init();
   for (i = 0; i < KEY_NUM; i++)
  {
    key[i].keydebugcnt=0;
    key[i].keystate=KEY_STATE0;
    key[i].keyval=KEY_NOPRESSVAL;
    key[i].keyvalstate=KEY_NOPRESSVAL;
    key[i].keyshortpress = KEY_SHORTPRESS;
    key[i].keylongpress = KEY_LONGPRESS;
    key[i].keylongpresscontinu=1;
  }
  //电源键不支持长按连发
  key[KEY_POWER_IDX].keylongpresscontinu=0;
  //读码、写码和OK键不支持长按连发
  key[KEY_READ_IDX].keylongpresscontinu=0;
  key[KEY_WRITE_IDX].keylongpresscontinu=0;
  key[KEY_OK_IDX].keylongpresscontinu=0;
}

/**
  * @brief  按键扫描
  * @param  
  * @param  10ms运行一次
  * @retval NUll
  */
void app_key_scan(KEY_t *key)
{
  uint32_t keyval, keyvaltemp;
  uint8_t i = 0;
  keyval = bsp_key_read();
  for (i = 0; i < KEY_NUM; i++)
  {
    keyvaltemp = (keyval >> i) & 0x01;
    switch (key[i].keystate)
    {
    case KEY_STATE0:
      if (keyvaltemp != 0) // 按键按下
      {
        key[i].keystate = KEY_STATE1;
        key[i].keyvalstate = keyvaltemp;
      }
      else
      {
        key[i].keydebugcnt = 0;
      }
      break;
    case KEY_STATE1: // 消抖
      if (keyvaltemp == key[i].keyvalstate)
      {
        key[i].keydebugcnt++;
        if (key[i].keydebugcnt > key[i].keyshortpress) // 短按
        {
          key[i].keystate = KEY_STATE2;
          key[i].keyval = KEY_SHORTPRESSVAL;
        }
      }
      else // 抖动
      {
        key[i].keystate = KEY_STATE0;
        key[i].keydebugcnt = 0;
      }
      break;
    case KEY_STATE2:                        /*等待短按释放*/
      if (keyvaltemp != key[i].keyvalstate) // 长按
      {
        key[i].keystate = KEY_STATE0;
        key[i].keydebugcnt = 0;
        key[i].keyval = KEY_RELEASEVAL;
      }
      else //
      {
        key[i].keydebugcnt++;
        if (key[i].keydebugcnt > key[i].keylongpress) //
        {
          key[i].keyval = KEY_LONGPRESSVAL;
          key[i].keydebugcnt = 0;
          key[i].keystate = KEY_STATE3;
        }
      }
      break;
    case KEY_STATE3:                        /*等待长按释放*/
      if (keyvaltemp == key[i].keyvalstate) // 长按
      {
        if (key[i].keylongpresscontinu == 1) //
        {
          key[i].keydebugcnt++;
          if (key[i].keydebugcnt > key[i].keyshortpress) //
          {
            key[i].keystate = KEY_STATE3;
            key[i].keyval = KEY_LONGPRESSVAL;
            key[i].keydebugcnt = 0;
          }
        }
      }
      else //
      {
        key[i].keystate = KEY_STATE0;
        key[i].keydebugcnt = 0;
        key[i].keyval = KEY_RELEASEVAL;
      }
      break;
    default:
      key[i].keystate = KEY_STATE0;
      key[i].keydebugcnt = 0;
      break;
    }
  }
}
/**
  * @brief  按键处理
  * @param  
  * @param  
  * @retval NUll
  */
void app_key_process(KEY_t *key)
{
  uint8_t i, j;

  app_key_scan(key);
  for (i = 0; i < KEY_NUM; i++)
  {
    if (key[i].keyval != KEY_NOPRESSVAL)
    {
      if (key[i].keyval == KEY_SHORTPRESSVAL)
      {
        switch (i)
        {
        case KEY_READ_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_READ_SHORT);
          break;
        case KEY_WRITE_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_WRITE_SHORT);
          break;
        case KEY_UP_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_UP_SHORT);
          break;
        case KEY_DOWN_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_DOWN_SHORT);
          break;
        case KEY_LEFT_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_LEFT_SHORT);
          break;
        case KEY_RIGHT_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_RIGHT_SHORT);
          break;
        case KEY_OK_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_OK_SHORT);
          break;
        case KEY_POWER_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_POWER_SHORT);
          break;
        case KEY_0_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_0_SHORT);
          break;
        case KEY_1_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_1_SHORT);
          break;
        case KEY_2_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_2_SHORT);
          break;
        case KEY_3_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_3_SHORT);
          break;
        case KEY_4_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_4_SHORT);
          break;
        case KEY_5_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_5_SHORT);
          break;
        case KEY_6_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_6_SHORT);
          break;
        case KEY_7_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_7_SHORT);
          break;
        case KEY_8_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_8_SHORT);
          break;
        case KEY_9_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_9_SHORT);
          break;
        default:
          break;
        }
        
      }
      else if (key[i].keyval == KEY_LONGPRESSVAL)
      {
        switch (i)
        {
        case KEY_READ_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_READ_LONG);
          break;
        case KEY_WRITE_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_WRITE_LONG);
          break;
        case KEY_UP_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_UP_LONG);
          break;
        case KEY_DOWN_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_DOWN_LONG);
          break;
        case KEY_LEFT_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_LEFT_LONG);
          break;
        case KEY_RIGHT_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_RIGHT_LONG);
          break;
        case KEY_OK_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_OK_LONG);
          break;
        case KEY_POWER_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_POWER_LONG);
          break;
        case KEY_0_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_0_LONG);
          break;
        case KEY_1_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_1_LONG);
          break;
        case KEY_2_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_2_LONG);
          break;
        case KEY_3_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_3_LONG);
          break;
        case KEY_4_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_4_LONG);
          break;
        case KEY_5_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_5_LONG);
          break;
        case KEY_6_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_6_LONG);
          break;
        case KEY_7_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_7_LONG);
          break;
        case KEY_8_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_8_LONG);
          break;
        case KEY_9_IDX:
          ui_lcd_postkeyval(&uitcb, UI_KEYVAL_9_LONG);
          break;
        default:
          break;
        }
      }
      key[i].keyval = KEY_NOPRESSVAL;
    }
  }
}
